﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;
namespace Nivdal
{
    public partial class FriendlyNT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            if (!IsPostBack)
            {
                if (Request.QueryString["Message"] != null)
                    MessageLabel.Text = Request.QueryString["Message"];

                MessagePanel.Visible = (MessageLabel.Text.Length > 0);

                if (ou.NationalTeamCoach != "0")
                {
                    NTScheduleDataSource.SelectParameters.Add("NTTeamId", ou.NationalTeamCoach);
                    NTScheduleDataSource.SelectParameters.Add("Language", ou.Language);
                    NTSuggestionsDataSourse.SelectParameters.Add("UserId", ou.UserId);
                    NTSuggestionsDataSourse.SelectParameters.Add("IsNT", "1");
                    NTChallangesDataSourse.SelectParameters.Add("UserId", ou.UserId);
                    NTChallangesDataSourse.SelectParameters.Add("IsNT", "1");

                    //NTIncomingSuggestionsPanel.Visible = (NTIncomingSuggestionsGridview.Rows.Count > 0);
                    //NTOutgoingChallangesPanel.Visible = (NTOutgoingFriendlyChallangesGridView.Rows.Count > 0);
                }
                else
                    Response.Redirect("~/TeamHomePage.aspx");                
            }

        }

        protected void IncomingFriendlyRowCommand(object sender, GridViewCommandEventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = "";
            if (e.CommandName == "Decline")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                Answer = GND.ReadNivdalStringData("ChallangeId", "Language", "Nivdal_FriendlyDeclineFriendlyChalange", e.CommandArgument, ou.Language);                
            }
            else if (e.CommandName == "Accept")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                Answer = GND.ReadNivdalStringData("ChallangeId", "Language", "Nivdal_FriendlyAcceptFriendlyChallange", e.CommandArgument, ou.Language);                
            }
            RefreshAll(Answer);
        }

        protected void WithdrawFriendly(object sender, GridViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = GND.ReadNivdalStringData("ChallangeId", "Language", "Nivdal_FriendlyDeleteFriendlyChalange", e.CommandArgument, ou.Language);
            RefreshAll(Answer);
        }

        protected string WithdrawChallangeOnClientClick(string TeamName)
        {
            return ("return confirm('" + Resources.Resource.Friendly_AreYouSureWithdrawFriendlyConfirm + " " + TeamName + " ?')");
        }

        protected string DeclineChallangeOnClientClick(string TeamName)
        {
            return ("return confirm('" + Resources.Resource.Friendly_AreYouSureDeclineFriendlyConfirm + " " + TeamName + " ?')");
        }

        protected string AcceptChallangeOnClientClick(string TeamName)
        {
            return ("return confirm('" + Resources.Resource.Friendly_AreYouSureAcceptFriendlyConfirm + " " + TeamName + " ?')");
        }

        protected string FormatWeekDate(object Time)
        {
            string returnValue = "";
            try
            {
                returnValue = string.Format("{0:d}", Time);
            }
            catch { returnValue = "error"; }

            return returnValue;
        }

        protected string FormatMatchTime(object Time)
        {
            string returnValue = "";
            try
            {
                returnValue = string.Format("{0:f}", Time);
            }
            catch { returnValue = "error"; }

            return returnValue;
        }

        protected string GetDatesString(object startDate, object EndDate)
        {
            return (Resources.Resource.Friendly_FriendlyWeekTitle + " " + startDate.ToString() + " - " + EndDate.ToString());

        }

        private void RefreshAll(string Message)
        {
            NtScheduleGridview.DataBind();
            NTIncomingSuggestionsGridview.DataBind();
            NTOutgoingFriendlyChallangesGridView.DataBind();
            
            MessageLabel.Text = Message;
            MessagePanel.Visible = (MessageLabel.Text.Length > 0);
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

    }
}